package com.ruoyi.web.controller.shop.shopuser.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.web.controller.shop.shopuser.domain.ShopRole;
import com.ruoyi.web.controller.shop.shopuser.domain.ShopUser;
import com.ruoyi.web.controller.shop.shopuser.mapper.ShopUserMapper;
import com.ruoyi.web.controller.shop.shopuser.service.IShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-10-30
 */
@Service
public class ShopUserServiceImpl implements IShopUserService 
{
    @Autowired
    private ShopUserMapper shopUserMapper;

    /**
     * 查询商户信息
     * 
     * @param userId 商户信息ID
     * @return 商户信息
     */
    @Override
    public Map<String,Object> selectShopUserById(Long userId)
    {
        return shopUserMapper.selectShopUserById(userId);
    }

    @Override
    public Map<String, Object> selectShopRoleByUserId(Long userId) {
        return shopUserMapper.selectShopRoleByUserId(userId);
    }

    @Override
    public ShopRole selectShopRoleById(Long roleId) {
        return shopUserMapper.selectShopRoleById(roleId);
    }

    /**
     * 查询商户信息列表
     * 
     * @param map 商户信息
     * @return 商户信息
     */
    @Override
    public List<Map<String,Object>> selectShopUserList(Map<String,Object> map)
    {
        return shopUserMapper.selectShopUserList(map);
    }

    @Override
    public List<Map<String, Object>> selectShopRoleList(Map<String, Object> map) {
        return shopUserMapper.selectShopRoleList(map);
    }

    @Override
    public List<Map<String, Object>> selectShopMenuList(Map<String, Object> map) {
        return shopUserMapper.selectShopMenuList(map);
    }

    @Override
    public List<Map<String, Object>> queryMenuByRoleId(Map<String,Object> map) {
        return shopUserMapper.queryMenuByRoleId(map);
    }

    /**
     * 新增商户信息
     * 
     * @param shopUser 商户信息
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertShopUser(ShopUser shopUser)
    {
        Long roleId = shopUser.getRoleId();
        if (roleId==null){
            return AjaxResult.error("请选择角色");
        }
        shopUser.setStatus("0");
        shopUser.setDelFlag("0");
        shopUser.setUserType("0");
        shopUser.setPassword(SecurityUtils.encryptPassword(shopUser.getPassword()));
        shopUser.setCreateTime(DateUtils.getNowDate());
        shopUserMapper.insertShopUser(shopUser);
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId.toString());
        map.put("suserId",shopUser.getSuserId().toString());
        shopUserMapper.insertShopUserRole(map);
        return AjaxResult.success("添加成功");
    }

    @Override
    @Transactional
    public AjaxResult insertShopRole(ShopRole shopRole) {
        List<Map<String,Object>> list = shopRole.getMenuList();
        if (list==null||list.size()<1){
            return AjaxResult.error("缺失菜单信息");
        }
        shopRole.setCreateBy(shopRole.getShopId());
        shopRole.setCreateTime(DateUtils.getNowDate());
        shopRole.setStatus(0);
        shopRole.setDelFlag(0);
        shopUserMapper.insertShopRole(shopRole);
        if (list.size()>0){
            for (Map<String,Object> m :list){
                m.put("roleId",shopRole.getRoleId().toString());
                shopUserMapper.insertShopRoleMenu(m);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改商户信息
     * 
     * @param shopUser 商户信息
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult updateShopUser(ShopUser shopUser)
    {
        Long roleId = shopUser.getRoleId();
        if (roleId==null){
            return AjaxResult.error("请选择角色");
        }
        shopUser.setUpdateTime(DateUtils.getNowDate());
        shopUserMapper.updateShopUser(shopUser);

        shopUserMapper.removeShopUserRoleByUserId(shopUser.getSuserId());
        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId.toString());
        map.put("suserId",shopUser.getSuserId().toString());
        shopUserMapper.insertShopUserRole(map);
        return AjaxResult.success("修改成功");
    }

    @Override
    public int resetPassword(ShopUser shopUser) {
        return shopUserMapper.resetPassword(shopUser);
    }

    @Override
    public int countUserName(String userName) {
        return shopUserMapper.countUserName(userName);
    }

    @Override
    @Transactional
    public AjaxResult updateShopRole(ShopRole shopRole) {
        List<Map<String,Object>> list = shopRole.getMenuList();
        if (list==null||list.size()<1){
            return AjaxResult.error("缺失菜单信息");
        }
        shopRole.setUpdateTime(DateUtils.getNowDate());
        shopRole.setUpdateBy(shopRole.getShopId());
        shopUserMapper.updateShopRole(shopRole);
        shopUserMapper.removeShopRoleMenuByRoleId(shopRole.getRoleId());
        if (list.size()>0){
            for (Map<String,Object> m :list){
                m.put("roleId",shopRole.getRoleId().toString());
                shopUserMapper.insertShopRoleMenu(m);
            }
        }
        return AjaxResult.success("修改成功");
    }

    /**
     * 批量删除商户信息
     * 
     * @param suserIds 需要删除的商户信息ID
     * @return 结果
     */
    @Override
    public int deleteShopUserByIds(Long[] suserIds)
    {
        return shopUserMapper.deleteShopUserByIds(suserIds);
    }

    /**
     * 删除商户信息信息
     * 
     * @param suserId 商户信息ID
     * @return 结果
     */
    @Override
    public int deleteShopUserById(Long suserId)
    {
        return shopUserMapper.deleteShopUserById(suserId);
    }

    @Override
    @Transactional
    public AjaxResult removeShopUser(Map<String, Object> map) {

        shopUserMapper.deleteShopUserById(Long.parseLong(map.get("suserId").toString()));
        shopUserMapper.removeShopUserRoleByUserId(Long.parseLong(map.get("suserId").toString()));
        return AjaxResult.success("删除成功");
    }

    @Override
    public AjaxResult removeShopRole(Map<String, Object> map) {

        int count = shopUserMapper.countRole(Long.parseLong(map.get("roleId").toString()));
        if (count>0){
            return AjaxResult.error("已分配角色不能删除");
        }
        shopUserMapper.removeShopRole(Long.parseLong(map.get("roleId").toString()));
        shopUserMapper.removeShopRoleMenuByRoleId(Long.parseLong(map.get("roleId").toString()));
        return AjaxResult.success("删除成功");
    }
}
